// models/article.js
const connection = require('../database'); // 假设这是您的数据库连接模块

const Article = {
    find: (callback) => {
        const query = 'SELECT * FROM articles'
        connection.query(query, [], callback)
    },
    create: (articleData, callback) => {
        const query = `INSERT INTO articles (title, content, status, user_id,username, img) VALUES (?, ?, ?, ?,?,?)`;
        connection.query(
            query,
            [
                articleData.title,
                articleData.content,
                articleData.status,
                articleData.user_id,
                articleData.username,
                articleData.img
            ],
            callback
        );
    },
    findById: (articleId, callback) => {
        const query = `SELECT * FROM articles WHERE id = ?`;
        connection.query(query, [articleId], callback);
    },
    findAll: (page, limit, callback) => {
        const offset = (page - 1) * limit;
        const query = `SELECT * FROM articles LIMIT ? OFFSET ?`;
        connection.query(query, [parseInt(limit), parseInt(offset)], callback);
    },
    update: (articleId, articleData, callback) => {
        console.log(JSON.stringify([articleData.title, articleData.content, articleData.status, articleId]));
        const query = `UPDATE articles SET title = ?, content = ?, status = ? WHERE id = ?`;
        connection.query(
            query,
            [articleData.title, articleData.content, articleData.status, articleId],
            callback
        );
    },
    delete: (articleId, callback) => {
        const query = `DELETE FROM articles WHERE id = ?`;
        connection.query(query, [articleId], callback);
    },
    // 其他可能的操作...
};

module.exports = Article;